﻿var c = 0;
var selectedProducts = "";
function s(sender) {
    if (sender.checked) {
        c++
        selectedProducts += sender.title + ":"
    }
    else {
        c--;
        selectedProducts.replace(sender.title + ":", "");
    }

        var msg = "";
        if (c == 1) {
            msg = "1 row selected";
        }
        else if (c == 0) {
            msg = "No rows selected";
        }
        else {
            msg = c + " rows selected";
        }
      
        $("#selectionCount")[0].innerHTML = msg;
        $("#selectedRows")[0].value = selectedProducts;
}

function filter() {
    var table = $("#ce")[0];
    var rowCount = table.rows.length;
    var columnCount = table.rows[0].cells.length;
    var selection = 0 ;
    var evenRow = 0;

    var articleIndex = 0;
  
    var sums = new Array();
    var strQty = $('#totalColumns')[0].value;
    
    var selectionEnabled = $("#selection").length > 0;
    

    var qtys = strQty.split(';');

    for (var i = 0; i < qtys.length; i++) {
        sums[i] = 0;
    } 
    
    for (var i = 2; i < rowCount - 1; i++) {
      var found = true;
        if (selectionEnabled) {
            var checked = $("#c" + (i-2))[0].checked;
            if (checked)
                selection++;
        }

           if (selectionEnabled && $("#chkFilter")[0].checked == true) {
               found = $("#c" + (i - 2))[0].checked;
            }

        for (var j = 1; j < columnCount; j++) {
            var columnName = table.rows[0].cells[j].innerHTML.replace(/<[^>]+>/g, "").replace("&nbsp;", "").replace("\n", "");
            var cell = table.rows[i].cells[j].innerHTML.replace(/<[^>]+>/g, "");
            if (columnName.toString().toLowerCase() == "selection")
            { break; }

            var filter = $("#filter" + j)[0].value;
            var filters = filter.toLowerCase().split(" ");

            if (columnName.toLowerCase() == "article")
                articleIndex = j;

            isNumeric = columnName.toLowerCase().indexOf('quantity') >= 0;


         

                if (isNumeric) {
                    var cellValue = parseFloat(cell.toString().replace('.', ''));
                    if (filter != "") {
                        if (filter.indexOf('>') >= 0) {
                            found = found && (cellValue > parseFloat(filter.replace('>', '')));
                        }
                        else if (filter.indexOf('<') >= 0) {
                            found = found && (cellValue < parseFloat(filter.replace('<', '')));
                        }
                        else if (filter.indexOf('-') >= 0) {
                            var limits = filter.split("-");
                            found = found && (cellValue >= parseFloat(limits[0]));
                            found = found && (cellValue <= parseFloat(limits[1]));
                        }
                        else {
                            found = found && (cellValue == parseFloat(filter));
                        }
                    }
                }

                else {
                    for (var k = 0; k < filters.length; k++) {
                        found = found && (cell.toLowerCase().indexOf(filters[k]) >= 0);
                    }
                    if (!found) {
                        break;
                    }
                }
            }
        


        if (found) {
            
            evenRow++;
            table.rows[i].style.display = '';
            if (evenRow % 2 == 0) {
                table.rows[i].style.background = '#f3f3f3';
             }
            else {
                table.rows[i].style.background = 'white';
            }


            for (var s = 0; s < qtys.length; s++) {
      
             var cellValue2 = parseFloat(table.rows[i].cells[parseInt(qtys[s])].innerHTML.replace('.', '')) ;
                sums[s] += parseFloat(cellValue2 );
            } 
        }
        else {
            table.rows[i].style.display = 'none';
        }
    }

    var lbltotals = $(".totals");

    for (var s = 0; s < qtys.length; s++) {
        lbltotals[s].innerHTML  = 'SUM = ' + accounting.formatNumber(sums[s], { precision: 2, thousand: ".", decimal: "," });
    }

    var articleTotal = $("#count");
    articleTotal[0].innerHTML = accounting.formatNumber(evenRow, { precision: 0, thousand: ".", decimal: "," }) + " ROWS";

    if (selectionEnabled) {
        var selectionCount = $("#selectionCount");
        selectionCount.innerHTML = selection;
        
    }
    //alert(selection);    
}